// pages/personalCenter/shopSite/shopSite.js；
import { addressPage,addressDelete } from '../../../common/api/my'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    list: [],
    isMore:false,
    type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    if(options){
      this.data.type = options.type;
    }
    this.getData()
  },
  // 页面滚到导致
  onReachBottom(){
    if(this.data.isMore){
      console.log('没有更多了')
      return
    }
    this.data.page+=1;
    this.getData();
  },
  onShareAppMessage(e) {
    let user = wx.getStorageSync('userInfo');
    console.log(e);
    return {
      title: "杆赢天下，求胜未来。一元乐享好球杆，千万球友的选择",
      imageUrl: "../../../images/shareImg.jpg",
       path: "/pages/index/index?id=" + user.id
    }
  },
  // 搜索地址分页
  async getData(){
    let user = wx.getStorageSync('userInfo');
    let res = await addressPage({
      name: '',
      page: this.data.page,
      size: 10,
      userId: user.id
    });
    if((res.list || []).length < 10){
      this.data.isMore = true;
    }
    (res.list || []).forEach(e =>{
      e.name2 = e.name.slice(0,1);
    })
    let a = [...this.data.list, ...(res.list || [])];
    this.setData({
      list: a
    })
  },
  // 删除单个
  async clear(e){
    let a = await wx.showModal({
      title: '确定要删除吗？'
    });
    console.log(a.confirm)
    if(!a.confirm){
      console.log('取消删除')
      return
    };
    let res = await addressDelete({id: e.currentTarget.dataset.item.id});
    wx.showToast({
      title: '删除成功',
      icon: "none"
    });
    // 重置
    this.reset();
  },
  // 编辑
  edit(e){
    let id = e.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: '/pages/personalCenter/updSite/updSite?id='+ id
    })
  },
  reset(){
    this.data.page = 1;
    this.data.isRome = false;
    this.setData({
      list: []
    });
    this.getData()
  },
  go:function(e){
    wx.navigateTo({
      url: '../updSite/updSite',
    })
  },
  goBack(e){
    
    if(this.data.type == 1){
      let item = e.currentTarget.dataset.item;
      const eventChannel = this.getOpenerEventChannel()
        eventChannel.emit('acceptDataFromOpenedPage', item);
        wx.navigateBack({
          delta: 1
        })
    }
    
  },
})